String sbuff;  // cadena que hace de buffer para el serial
int max_chars = sizeof(char) * 128; // maximo tamano de cadena
int pin_a = 7; // D7
int pin_b = 8; // D8
unsigned long dura_global =0;
unsigned long durb_global =0;

void setup ()
{
  // pin setup
  pinMode     (pin_a, OUTPUT);
  pinMode     (pin_b, OUTPUT);
  digitalWrite(pin_a, LOW);
  digitalWrite(pin_b, LOW);
  // string setup
  sbuff.reserve (max_chars);
  // serial setup
  Serial.begin(9600);
  // mensaje de listo
  Serial.println("*READY*");
}

void loop ()
{
  delay (1);
  
  if (dura_global)
  {
    digitalWrite(pin_a, HIGH);
    dura_global --;
  }
  else
  {
    digitalWrite(pin_a, LOW);
  }
  
  if (durb_global)
  {
    digitalWrite(pin_b, HIGH);
    durb_global --;
  }
  else
  {
    digitalWrite(pin_b, LOW);
  }
}

void procesarComando ()
{  
  String err = "err cmd";
  int index = 0;
  
  switch (sbuff[index])
  {
    case '?':
      {
        // formato ?(a|b|x)\0
        index++;
        
        switch (sbuff[index])
        {
          case '\0':
            Serial.println(err);
            break;
          case 'a':
            Serial.print  ("dura: ");
            Serial.println(dura_global);
            break;
          case 'b':
            Serial.print  ("durb: ");
            Serial.println(durb_global);
            break;
          case 'x':
            Serial.print  ("dura: ");
            Serial.print  (dura_global);
            Serial.print  (" durb: ");
            Serial.println(durb_global);
            break;
        }
      }
      break;
    case 'a':
      {
        // formato a:nnn
        index++;
        if (sbuff[index] != ':')
        {
          Serial.println(err);
          break;
        }
        
        index++;
        unsigned long dura = 0;
        while ((sbuff[index] >= '0') && (sbuff[index] <= '9'))
        {
          dura = (dura * 10) + (sbuff[index] - '0');
          index++;
        }
        
        //if (sbuff[index] != '\0')
        //{
        //  Serial.println(err);
        //  break;
        //}
        
        dura_global = dura;
        
        Serial.print   ("dura: ");
        Serial.println (dura);
      }
      break;
      
    case 'b':
      {
        // formato b:nnn
        index++;
        if (sbuff[index] != ':')
        {
          Serial.println(err);
          break;
        }
        
        index++;
        unsigned long durb = 0;
        while ((sbuff[index] >= '0') && (sbuff[index] <= '9'))
        {
          durb = (durb * 10) + (sbuff[index] - '0');
          index++;
        }
        
        //if (sbuff[index] != '\0')
        //{
        //  Serial.println(err);
        //  break;
        //}
        
        durb_global = durb;
        
        Serial.print   ("durb: ");
        Serial.println (durb);
      }
      break;
      
    case 'x':
      { 
        // formato x:nnn:nnn
        index++;
        if (sbuff[index] != ':')
        {
          Serial.println(err);
          break;
        }
        
        index++;
        unsigned long dura = 0;
        while ((sbuff[index] >= '0') && (sbuff[index] <= '9'))
        {
          dura = (dura * 10) + (sbuff[index] - '0');
          index++;
        }
        
        if (sbuff[index] != ':')
        {
          Serial.println(err);
          break;
        }
        index++;

        unsigned long durb = 0;
        while ((sbuff[index] >= '0') && (sbuff[index] <= '9'))
        {
          durb = (durb * 10) + (sbuff[index] - '0');
          index++;
        }
      
        //if (sbuff[index] != '\0')
        //{
        //  Serial.println(err);
        //  break;
        //}
      
        dura_global = dura;
        durb_global = durb;

        Serial.print   ("dura: ");
        Serial.print   (dura);
        Serial.print   (' ');
        Serial.print   ("durb: ");
        Serial.println (durb);        
      }
      break;
    default:
      {
        Serial.println(err);
      }
      break;
  }
}

void serialEvent ()
{
  int byteRead = Serial.read();
  
  switch (byteRead)
  {
    case 13: // CR
    case 10: // LF
      if (sbuff.length() > 0)
      {
        procesarComando();
        sbuff = "";
      }
      break;
    default:
      sbuff += (char) byteRead;
      break;
  }
  
  if ((sbuff.length()+1) == max_chars)
    sbuff = "";
}


